var Advertising = function(){
    var base_url = "";
    var url_reg = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;
    var datepicker=function(){
        $('.form_date').datetimepicker({
            language: 'zh-CN',//显示中文
            format: 'yyyy-mm-dd',//显示格式
            minView: "month",//设置只显示到月份
            initialDate: new Date(),
            startDate:new Date(),
            MinDate:new Date(),
            autoclose: true,//选中自动关闭
            todayBtn: true,//显示今日按钮
        });

        $('.js-open-time').datetimepicker().on('changeDate', function(ev){
            var date = $('#open_time').val();
            if(date && date !=="undefined"){
                $(".js-close-time").datetimepicker("setStartDate", new Date(date));
            }
        });

        $('.js-close-time').datetimepicker().on('changeDate', function(ev){
            var date = $('#close_time').val();
            if(date && date !=="undefined"){
                $(".js-open-time").datetimepicker("setEndDate", new Date(date));
            }

        });
    }

    var advertisingAdd = function(){
        $('.js-add').on('click',function(){
            var  param      = "",
                 ad_name    = $('#ad_name').val(),
                 logo       = new FormData($('#add_uploadForm')[0]),
                 open_time  = $('#add_open_time').val(),
                 close_time = $('#add_close_time').val(),
                 media_id   = $('#add_media').val(),
                 link       = $('#add_ad_link').val();

            if(!ad_name || ad_name ==="undefine" || ad_name==null){
                $('.js-add-ad-name').text("请输入广告名称").removeClass('hide');
                return;
            }else{
                if(!$('.js-add-ad-name').hasClass('hide')){
                    $('.js-add-ad-name').text("").addClass('hide');
                }
            }

            if(!url_reg.test(link)){
                $('.js-add-ad-link').text("请输入正确的广告地址").removeClass('hide');
                return;
            }else{
                if(!$('.js-add-ad-link').hasClass('hide')){
                    $('.js-add-ad-link').text("").addClass('hide');
                }
            }

            if(!open_time || open_time ==="undefine" || open_time==null){
                $('.js-add-open-time').text("请选择投放开始日期").removeClass('hide');
                return;
            }else{
                if(!$('.js-add-open-time').hasClass('hide')){
                    $('.js-add-open-time').text("").addClass('hide');
                }
            }

            if(!close_time || close_time ==="undefine" || close_time==null){
                $('.js-add-close-time').text("请选择投放结束日期").removeClass('hide');
                return;
            }else{
                if(!$('.js-add-close-time').hasClass('hide')){
                    $('.js-add-close-time').text("").addClass('hide');
                }
            }


            param  += "?ad_name="+ad_name;
            param  += "&link="+link;
            param  += "&open_time="+open_time;
            param  += "&close_time="+close_time;
            param  += "&media_id="+media_id;

            $.ajax({
                url: base_url+apis.advertisingStore_url+param,
                type: 'POST',
                data: logo,
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                dataType:"json",
                success: function (result) {
                    if(result.code==1){
                        alert("添加成功");
                        $('#ModalTypeEdit').modal("hide");
                        window.location.reload();
                    }else{
                        alert(result.msg);
                    }
                },
                error: function (data) {

                }
            });
        })
    }

    var advertisingEdit = function(){
        $('.js-edit').on('click',function(){
            $('#ad_id').val($(this).parent().parent().data('id'));
            $('#edit_ad_name').val($(this).parent().siblings().eq(1).text());
            $('#picImg_edit').attr("src",$(this).parent().siblings().eq(2).find('img').attr("src"));
            $('#edit_ad_link').val($(this).parent().siblings().eq(3).text());
            $('#edit_media').val($(this).parent().siblings().eq(4).data('id'));
            $('#edit_open_time').val($(this).parent().siblings().eq(5).text());
            $('#edit_close_time').val($(this).parent().siblings().eq(6).text());
            $('#edit_state').val($(this).parent().siblings().eq(7).data('id'));
            $("#ModalEdit").modal("show");
        })
        $('.js-save').on('click',function(){
            var  param      = "",
                ad_id      = $('#ad_id').val(),
                logo       = new FormData($('#edit_uploadForm')[0]),
                open_time  = $('#edit_open_time').val(),
                close_time = $('#edit_close_time').val(),
                media_id   = $('#edit_media').val(),
                link       = $('#edit_ad_link').val(),
                status     = $('#edit_status').val();


            if(!url_reg.test(link)){
                $('.js-edit-ad-link').text("请输入正确的广告地址").removeClass('hide');
                return;
            }else{
                if(!$('.js-edit-ad-link').hasClass('hide')){
                    $('.js-edit-ad-link').text("").addClass('hide');
                }
            }

            if(!open_time || open_time ==="undefine" || open_time==null){
                $('.js-edit-open-time').text("请选择投放开始日期").removeClass('hide');
                return;
            }else{
                if(!$('.js-edit-open-time').hasClass('hide')){
                    $('.js-edit-open-time').text("").addClass('hide');
                }
            }

            if(!close_time || close_time ==="undefine" || close_time==null){
                $('.js-edit-close-time').text("请选择投放结束日期").removeClass('hide');
                return;
            }else{
                if(!$('.js-edit-close-time').hasClass('hide')){
                    $('.js-edit-close-time').text("").addClass('hide');
                }
            }


            param  += "?ad_id="+ad_id;
            param  += "&link="+link;
            param  += "&open_time="+open_time;
            param  += "&close_time="+close_time;
            param  += "&media_id="+media_id;
            param  += "&status="+status;

            $.ajax({
                url: base_url+apis.advertisingStore_url+param,
                type: 'POST',
                data: logo,
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                dataType:"json",
                success: function (result) {
                    if(result.code==1){
                        alert("修改成功");
                        $('#ModalEdit').modal("hide");
                        window.location.reload();
                    }else{
                        alert(result.msg);
                    }
                },
                error: function (data) {

                }
            });
        })
    }


    return {
        //main function to initiate template pages
        init: function (baseUrl) {
            base_url = baseUrl;
            datepicker();
            advertisingAdd();
            advertisingEdit();
        }
    }
}();